package com.szholly.pro.watergas_server.ps.controller;

import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import com.szholly.plug.activity.core.BpmException;
import org.activiti.engine.ActivitiObjectNotFoundException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.jdbc.UncategorizedSQLException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.szholly.pro.watergas_server.common.data.DataUtil;
import com.szholly.pro.watergas_server.common.util.JsonUtil;
import com.szholly.pro.watergas_server.common.web.BaseController;
import com.szholly.pro.watergas_server.common.web.MessageException;
import com.szholly.pro.watergas_server.common.web.RequestUnit;
import com.szholly.utils.spring.SpringBeanFactory;
import com.szholly.data.general.IWorkspace;
import com.szholly.data.general.IWorkspaceFactory;
import com.szholly.data.general.RowBase;
import com.szholly.data.general.interceptor.defulat.UniqueCheckException;
import com.szholly.data.general.interceptor.defulat.VersionCheckException;
import com.szholly.plug.form.validation.FieldIsNullException;
import com.szholly.plug.form.validation.RegexNotMatchException;
import com.szholly.plug.form.validation.StringLengthException;

/**
 * 数据操作controller，提供通用的表单保存，修改和删除的接口
 * @author gislin
 * 2015年12月21日
 */
@Controller
@RequestMapping("/Data/*")
public class DataController extends BaseController { 
	
	protected Logger logger = LoggerFactory.getLogger(getClass());	
	
	@RequestMapping (value = "/getHtml")
	public void getHtml(@RequestParam String tableName,HttpServletResponse res){
		String sql = "select * from user_col_comments where Table_Name='"+tableName+"'";
		IWorkspaceFactory workspaceFactory = (IWorkspaceFactory) SpringBeanFactory.getBean("WorkspaceFactory");
		IWorkspace pWorkspace = workspaceFactory.openWorkspace();

		List<RowBase> list =  pWorkspace.getEntityList(RowBase.class,sql,null);
		//pWorkspace.close();
		String result = "";
		for(RowBase row:list){
			String label = row.getValue("COMMENTS")+"";
			String fieldName = row.getValue("COLUMN_NAME")+"";
			result += fieldName + ":" +label+";";
		}
		outString(result);
	}
	
	/**
	 * 新增
	 * gislin
	 * 2015年12月21日
	 * @param res
	 */
	@RequestMapping (value = "/Create")
	public void Create(HttpServletResponse res){
		try {
			HashMap<String, Object> parms = RequestUnit.getHashMapParmO();
			String tableName = RequestUnit.getString("tableName");
			String guid = DataUtil.Create(tableName, parms);
			String jsonStr = "{\"data\":\"success\",\"GUID\":\""+guid+"\"}";
			outString(jsonStr);
		} catch (DuplicateKeyException dx){
			dx.printStackTrace();
			outErrorString("主键冲突。");
		} catch (BpmException ux){
			ux.printStackTrace();
			outErrorString(ux.getMessage());
		} catch (UncategorizedSQLException ux){
			ux.printStackTrace();
			outErrorString("数据执行错误。");
		} catch (UniqueCheckException ux){
			ux.printStackTrace();
			outErrorString(ux.getMessage());
		} catch (VersionCheckException ux){
			ux.printStackTrace();
			outErrorString(ux.getMessage());
		} catch (ActivitiObjectNotFoundException ax){
			ax.printStackTrace();
			outErrorString("流程实例已经结束。");
		} catch (FieldIsNullException e){
			e.printStackTrace();
			outErrorString( e.getMessage());
		} catch (RegexNotMatchException e){
			e.printStackTrace();
			outErrorString(e.getMessage());
		} catch (StringLengthException e){
			e.printStackTrace();
			outErrorString(e.getMessage());
		}catch (MessageException e) {
			outErrorString(e.getMessage());
		}catch (Exception ex) {
			ex.printStackTrace();
			outErrorString(ex.getMessage());
		}
	}
	
	/**
	 * 获取model
	 * gislin
	 * 2016年11月8日
	 * @param res
	 */
	@RequestMapping (value = "/getModelByKey")
	public void getModelByKey(HttpServletResponse res){
		try {
			String key = RequestUnit.getString("key");
			String tableName = RequestUnit.getString("tableName");
			RowBase model = DataUtil.getModelByKey(tableName, key);
			String jsonStr = JsonUtil.ConvertObjectToJson(model);
			outString(jsonStr);
		} catch (Exception ex) {
			ex.printStackTrace();
			outErrorString("数据执行错误。");
		}
	}
	
	/**
	 * 更新
	 * gislin
	 * 2015年12月21日
	 * @param res
	 */
	@RequestMapping (value = "/Update")
	public void Update(HttpServletResponse res){
		try {
			HashMap<String, Object> parms = RequestUnit.getHashMapParmO();
			String tableName = RequestUnit.getString("tableName");
			DataUtil.Update(tableName, parms);
			String jsonStr = "{\"data\":\"success\"}";
			outString(jsonStr);
		} catch (DuplicateKeyException dx){
			dx.printStackTrace();
			outErrorString("主键冲突。");
		} catch (BpmException ux){
			ux.printStackTrace();
			outErrorString(ux.getMessage());
		} catch (UncategorizedSQLException ux){
			ux.printStackTrace();
			outErrorString("数据执行错误。");
		} catch (UniqueCheckException ux){
			ux.printStackTrace();
			outErrorString(ux.getMessage());
		} catch (VersionCheckException ux){
			ux.printStackTrace();
			outErrorString(ux.getMessage());
		} catch (ActivitiObjectNotFoundException ax){
			ax.printStackTrace();
			outErrorString("流程实例已经结束。");
		} catch (FieldIsNullException e){
			e.printStackTrace();
			outErrorString( e.getMessage());
		} catch (RegexNotMatchException e){
			e.printStackTrace();
			outErrorString(e.getMessage());
		} catch (StringLengthException e){
			e.printStackTrace();
			outErrorString(e.getMessage());
		} catch (MessageException e) {
			outErrorString(e.getMessage());
		}catch (Exception ex) {
			ex.printStackTrace();
			outErrorString(ex.getMessage());
		}
	}
	
	
	/**
	 * 删除
	 * gislin
	 * 2016年11月8日
	 * @param res
	 */
	@RequestMapping (value = "/Delete")
	public void Delete(HttpServletResponse res){
		try {
			String key = RequestUnit.getString("key");
			String tableName = RequestUnit.getString("tableName");
			DataUtil.Delete(tableName, key);
			String jsonStr = "{\"data\":\"success\"}";
			outString(jsonStr);
		} catch (MessageException e) {
			outErrorString(e.getMessage());
		}catch (Exception ex) {
			ex.printStackTrace();
			outErrorString("数据执行错误。");
		}
	}
	
	@RequestMapping (value = "/getCurrentUser")
	public void getCurrentUser(HttpServletResponse res){
		String jsonStr = getCurrentUserJsonString();
		outString(jsonStr);
	}
	
}
